﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using ShredTheLog.Api;
using System.Windows.Input;
using ShredTheLog.Common.MVVM;
using ShredTheLog.Views;
using ShredTheLog.ViewModels;
using ShredTheLog.Api.DataContracts;
using System.Diagnostics;
using System.Windows.Media;
using System.Diagnostics.Contracts;
using ShredTheLog.Controls;
using System.Windows;

namespace ShredTheLog.Plugins
{
    [Export(typeof(ICommandListItem))]
    [MenuCommandMetadata(Menu = Menus.File, Order = 0, Group = null)]
    [Export(typeof(IShowLogInDialogCommand))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public sealed class ShowLogInDialogCommand : DelegateCommand, IUiCommand, IShowLogInDialogCommand
    {
        [Import]
        private IShredServices ShredServices { get; set; }

        public ShowLogInDialogCommand()
        {
            _executeMethod =
                () =>
                {
                    var window =
                        (from i in Application.Current.Windows.Cast<Window>()
                         let vm = i.DataContext as LogInViewModel
                         where vm != null
                         select i).SingleOrDefault();
                    if (window == null)
                        new LogInView().Show(new LogInViewModel(c => Credentials = c));
                    else
                        window.Activate();
                };
            _canExecuteMethod = () => this.Credentials == null;
        }

        public string Name { get { return ShredServices.GetLocalizedString("s_Menu_LogIn"); } }
        public ImageSource Image { get { return null; } }

        public ISessionCredentials Credentials 
        {
            get { return _credentials; }
            set 
            { 
                _credentials = value;
                if (CredentialsChanged != null)
                    CredentialsChanged(this, EventArgs.Empty);
            }
        }
        private ISessionCredentials _credentials;
        public event EventHandler CredentialsChanged;
    }

    [Export(typeof(ICommandListItem))]
    [MenuCommandMetadata(Menu = Menus.File, Order = 100, Group = null)]
    [Export(typeof(ILogOutCommand))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public sealed class LogOutCommand : DelegateCommand, IUiCommand, ILogOutCommand
    {
        [Import]
        private IShredServices ShredServices { get; set; }

        [ImportingConstructor]
        public LogOutCommand(IShowLogInDialogCommand logInCommand)
            : base(() => logInCommand.Credentials = null, () => logInCommand.Credentials != null)
        {
            Contract.Requires(logInCommand != null);
        }

        public string Name { get { return ShredServices.GetLocalizedString("s_LogOut"); } }
        public ImageSource Image { get { return null; } }
    }

    [Export(typeof(ICommandListItem))]
    [MenuCommandMetadata(Menu = Menus.File, Order = 200, Group = null)]
    [Export(typeof(IRegisterCommand))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public sealed class RegisterCommand : DelegateCommand, IUiCommand, IRegisterCommand
    {
        [Import]
        private IShredServices ShredServices { get; set; }

        public RegisterCommand()
            : base(() => Process.Start(@"http://sharedtalk.com/"))
        {
        }

        public string Name { get { return ShredServices.GetLocalizedString("s_Register"); } }
        public ImageSource Image { get { return null; } }
    }

    [Export(typeof(ICommandListItem))]
    [MenuCommandMetadata(Menu = Menus.File, Order = 300, Group = null)]
    [Export(typeof(IEditProfileCommand))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public sealed class EditProfileCommand : DelegateCommand, IUiCommand, IEditProfileCommand
    {
        [Import]
        private IShredServices ShredServices { get; set; }

        public EditProfileCommand()
            : base(() => { throw new NotImplementedException(); }, () => false)
        {
        }

        public string Name { get { return ShredServices.GetLocalizedString("s_EditProfile"); } }
        public ImageSource Image { get { return null; } }
    }

    [Export(typeof(ICommandListItem))]
    [MenuCommandMetadata(Menu = Menus.File, Order = 400, Group = ExitCommandGroup)]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public sealed class ExitCommand : DelegateCommand, IUiCommand
    {
        private readonly IShredServices _shredServices;

        public const string ExitCommandGroup = "Exit";

        [ImportingConstructor]
        public ExitCommand(IShredServices shredServices)
            : base(() => shredServices.Shutdown())
        {
            Contract.Requires(shredServices != null);

            _shredServices = shredServices;
        }

        public string Name { get { return _shredServices.GetLocalizedString("s_Exit"); } }
        public ImageSource Image { get { return null; } }
    }
}
